﻿using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Data;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Text;
using CommDll;
using System.Xml.Linq;
using System.Xml;
using System.Drawing;
using System.ComponentModel;
using DevComponents.DotNetBar.Controls;
namespace InstitutionAssitant
{
    /// <summary>
    /// ClassName:frmQuery_EnterpriseCodeCheck0
    /// Version:1.0
    /// Date:2019-06-23
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为实现企业对码(先确定平台企业信息)
    /// </remarks>
    public partial class frmQuery_EnterpriseCodeCheck0 : DevComponents.DotNetBar.OfficeForm
    {
        #region 01 自定义属性
        public Dictionary<string, string> m_dictInfo = new Dictionary<string, string>(); // 回传信息(指定对码时用)
        #endregion
        public frmQuery_EnterpriseCodeCheck0()
        {
            InitializeComponent();
        }
  
        #region 02 自定义方法
        /// <summary>
        /// 初始化DataGridView字段显示信息
        /// </summary>
        public void _initDgvTitle()
        {
            string strInfo = "";
            string strKind = "";
            strKind = this.Name + "0";
            // 存在时不再处理
            if (AppConfig.m_dictFieldName.ContainsKey(strKind))
            {
                return;
            }
            strInfo = "ID-序号,Code-HIS配企编码,Name-HIS配企名称,SupplierCode-配企编码,SupplierName-配企名称,Linkman-业务员,CheckCodeTime-对码时间";
            AppConfig.m_dictFieldName.Add(strKind, strInfo);
            strInfo = "ID,Code,Name,SupplierCode,SupplierName,Linkman,CheckCodeTime";
            AppConfig.m_dictFieldSql.Add(strKind, strInfo);
            strInfo = "ID,Code,Name,SupplierCode,SupplierName,Linkman,CheckCodeTime";
            AppConfig.m_dictField0.Add(strKind, strInfo);
            strInfo = "0,100,300,100,300,80,200";
            AppConfig.m_dictFieldWidth0.Add(strKind, strInfo);
            // 设置值
            strInfo = AppConfig.m_iniSetup._readValue("Common", string.Format("{0}_Field", strKind), AppConfig.m_dictField0[strKind]).Trim();
            AppConfig.m_dictField.Add(strKind, strInfo);
            strInfo = AppConfig.m_iniSetup._readValue("Common", string.Format("{0}_FieldWidth", strKind), AppConfig.m_dictFieldWidth0[strKind]).Trim();
            AppConfig.m_dictFieldWidth.Add(strKind, strInfo);
        }
        
        /// <summary>
        /// 查询HIS配企业息
        /// </summary>
        private void _queryCustomer1()
        {
            string strWhere = "";
            string strTemp = "";
            try
            {
                // 01 取条件
                // 0101 通用条件
                strTemp = txtWhere0.Text.Trim();
                if (!strTemp.Equals("") && (cbWhere01.Checked || cbWhere02.Checked))
                {
                    if (cbWhere01.Checked)
                    {
                        strWhere = (strWhere.Equals("") ? "" : " and ") + string.Format("(Code like '%{0}%' or SupplierCode like '%{0}%')", strTemp);
                    }
                    if (cbWhere02.Checked)
                    {
                        strWhere += (strWhere.Equals("") ? "" : " and ") + string.Format("(Name like '%{0}%' or SupplierName like '%{0}%')", strTemp);
                    }
                }
                // 0102 对码状态
                strTemp = "";
                //if (cbState0.Checked || cbState1.Checked || cbState.Checked)
                //{
                //    if (cbState0.Checked)
                //    {
                //        strTemp = (strTemp.Equals("") ? "" : " or ") + string.Format("CheckCodeStatus = {0} ", 0);
               //     }
                 //   if (cbState1.Checked)
                 //   {
                 //       strTemp += (strTemp.Equals("") ? "" : " or ") + string.Format("CheckCodeStatus = {0} ", 1);
                 //   }
                 //   if (cbState.Checked)
                 //   {
                 //       strTemp += (strTemp.Equals("") ? "" : " or ") + string.Format("CheckCodeStatus = {0} ", -1);
                 //   }
                 //   strWhere += (strWhere.Equals("") ? "" : " and ") + string.Format("({0})", strTemp);
                //}
                strWhere = (strWhere.Equals("") ? "" : " where ") + string.Format("{0}", strWhere);

                // 02 取数据
                string strSql = string.Format("SELECT top 1000 {0} FROM V_SupplierCheck {{0}} order by ID", AppConfig.m_dictFieldSql[this.Name + "0"]);
                strSql = string.Format(strSql, strWhere);
                DataTable dt = DbHelper.DBAccess[0]._openSql_Dt(strSql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    // 03 显示数据
                    // 0301 调整字段顺序
                    CommFun._dataTableFieldMove(dt, AppConfig.m_dictField[this.Name + "0"]);
                    // 0302 显示数据
                    CommFun._showDataGridView0(dgv0, dt, AppConfig.m_dictFieldWidth[this.Name + "0"], AppConfig.m_dictFieldName[this.Name + "0"]);                    
                }
                else
                {
                    // 清除数据
                    DataTable dtTemp = (DataTable)dgv0.DataSource;
                    if (dtTemp != null && dtTemp.Rows.Count > 0)
                    {
                        dtTemp.Rows.Clear();
                    }
                }
            }
            catch (Exception ex)
            {
                CommFun._messageBoxShow(ex.Message);
                return;
            }
        }

        /// <summary>
        /// 对码
        /// </summary>
        private void _checkCode()
        {
            try
            {
                frmSupplierCodeCheck frm = new frmSupplierCodeCheck();
                // 取联系人
                // 去掉原联系人
                if (!string.IsNullOrWhiteSpace(ModelSupplierCodeCheck.strSupplierCode) && !ModelSupplierCodeCheck.strSupplierCode.Equals(ModelSupplierCodeCheck.strCode1))
                {
                    ModelSupplierCodeCheck.strLinkman0 = "";
                }
                
                if (!string.IsNullOrWhiteSpace(ModelSupplierCodeCheck.strLinkman1) && cbIsBySalesman.Checked)
                {
                    if (!string.Format(",{0},", ModelSupplierCodeCheck.strLinkman0).Contains(string.Format(",{0},", ModelSupplierCodeCheck.strLinkman1)))
                    {
                        ModelSupplierCodeCheck.strLinkman0 += ((string.IsNullOrWhiteSpace(ModelSupplierCodeCheck.strLinkman0)) ? "" : ",") + ModelSupplierCodeCheck.strLinkman1;
                    }
                }
                
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    // 更改原对码信息   
                    string strTemp = m_dictInfo["SupplierCode_His"];
                    if (!string.IsNullOrWhiteSpace(strTemp) && (!strTemp.Equals(ModelSupplierCodeCheck.strCode0)))
                    {
                        string strSql = "Update T_Base_SupplierCodeCheck set SupplierCode= '',Linkman = '' ,CheckCodeStatus = 0 Where Code = '{0}'";
                        strSql = string.Format(strSql, strTemp);
                        DbHelper.DBAccess[0]._execSql(strSql);
                    }
                    DialogResult = DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                CommFun._messageBoxShow(ex.Message);
                return;
            }
        }
        #endregion

        #region 03 窗体相关事件方法

        private void frmQuery_EnterpriseCodeCheck_Load(object sender, EventArgs e)
        {
            timerInit.Enabled = true;
            dgv0.Anchor = (System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Top));
        }
        private void timerInit_Tick(object sender, EventArgs e)
        {
            timerInit.Enabled = false;
            // 初始化DataGridView
            _initDgvTitle();
            //
            // 初始化控件信息
            txtSupplierCode.Text = ModelSupplierCodeCheck.strCode1;
            txtSupplierName.Text = ModelSupplierCodeCheck.strName1;
            txtSupplierLinkman.Text = ModelSupplierCodeCheck.strLinkman1;
            txtGoodsName.Text = m_dictInfo["Name_His"];
            txtGoodsSpec.Text = m_dictInfo["Spec_His"];
            txtGoodsUnit.Text = m_dictInfo["Unit_His"];
            txtGoodsManufacturer.Text = m_dictInfo["Manufacturer_His"];
            // 取初始化数据
            txtSupplierCode_Click(txtSupplierName, null);
        }

        private void btnQuery1_Click(object sender, EventArgs e)
        {
            _queryCustomer1();
        }

        private void txtWhere1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnQuery1_Click(null, null);
            }
        }
        private void txtSupplierCode_Click(object sender, EventArgs e)
        {
            // 快速查找数据
            TextBox tb = (TextBox)sender;
            cbWhere01.Checked = tb.Name.Equals("txtSupplierCode") ? true : false;
            cbWhere02.Checked = !cbWhere01.Checked;
            txtWhere0.Text = tb.Text;
            btnQuery1_Click(null, null);
        }
        #endregion

        #region  DataGridView 事件方法

        private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            CommFun._dgvColumnHeaderSort(sender, e);
        }

        private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            // 对码
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }
            DataGridView dgv = (DataGridView)sender;
            // 初始化数据
            ModelSupplierCodeCheck.strID0 = dgv.Rows[e.RowIndex].Cells["ID"].Value.ToString();
            ModelSupplierCodeCheck.strCode0 = dgv.Rows[e.RowIndex].Cells["Code"].Value.ToString();
            ModelSupplierCodeCheck.strName0 = dgv.Rows[e.RowIndex].Cells["Name"].Value.ToString();
            ModelSupplierCodeCheck.strLinkman0 = dgv.Rows[e.RowIndex].Cells["Linkman"].Value.ToString(); //txtSupplierLinkman.Text.Trim(); 

            ModelSupplierCodeCheck.strSupplierCode = dgv.Rows[e.RowIndex].Cells["SupplierCode"].Value.ToString();//txtSupplierCode.Text.Trim(); 
            // 对码
            _checkCode();                   
        }
        
        private void dataGridViewX1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            // 改变行颜色
            CommFun._dgvRowEnter(sender, e);
        }
     
        private void dataGridView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // 设置标题信息    
            if (e.Button == MouseButtons.Right)
            {
                DataGridView dgv = (DataGridView)sender;
                frmDataShowSetup frm = new frmDataShowSetup();
                switch (dgv.Name)
                {
                    case "dgv0":
                        frm.m_strInfoKind = this.Name + "0";
                        if (frm.ShowDialog() == DialogResult.OK)
                        {
                            if (frm.m_bIsSaveWidth)
                            {
                                // 保存宽度
                                AppConfig._saveDgvWidth(dgv0, frm.m_strInfoKind, true);
                                CommFun._messageBoxShow("显示宽度保存成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {
                                if (dgv0.Rows.Count > 0)
                                {
                                    btnQuery1_Click(null, null);
                                }
                            }
                        }
                        break;
                }
            }
        }
        #endregion
    }
}
